layui.use(['form', 'table','miniPage','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        miniPage = layui.miniPage;

    var tableIns = table.render({
        elem: '#currentTableId',
        url: 'carInfo/list',
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [[
            {type: "checkbox", width: 50},
            {field: 'id', width: 80, title: 'ID', sort: true},
            {field: 'name', width: 100, title: '名称'},
            {field: 'price', width: 80, title: '价格', sort: true},
            {field: 'carType',width: 80, title: '车型'},
            {field: 'firstUpTime', width: 200, title: '第一次上牌时间',templet:function (d){
                var time1 = DateFormat(new Date(d.firstUpTime),"yyyy-MM-dd hh:mm:ss");
                return time1;
            }
            },
            {field: 'photo', width: 100, title: '图片', sort: true,templet:function (d) {
                    return ' <img src="'+d.photo+'" style="width: 20px">'
                }},
            // {field: 'score', width: 80, title: '评分', sort: true},
            // {field: 'classify', width: 80, title: '职业'},
            // {field: 'wealth', width: 135, title: '财富', sort: true},
            {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true,
        skin: 'line'
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        var result = JSON.stringify(data.field);
        layer.alert(result, {
            title: '最终的搜索信息'
        });

        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    /**
     * toolbar事件监听
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add') {   // 监听添加操作
            var content = miniPage.getHrefContent('page/business/carInfo_add.html');
            var openWH = miniPage.getOpenWidthHeight();

            var index = layer.open({
                title: '增加车辆信息',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: [openWH[0]/2 + 'px', openWH[1] + 'px'],
                // maxWidth:'50%',
                offset:'auto',
                // offset: [openWH[2] + 'px', openWH[3] + 'px'],
                content: content,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {

            var content = miniPage.getHrefContent('page/table/add.html');
            var openWH = miniPage.getOpenWidthHeight();

            var index = layer.open({
                title: '编辑用户',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: [openWH[0] + 'px', openWH[1] + 'px'],
                offset: [openWH[2] + 'px', openWH[3] + 'px'],
                content: content,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        }
    });
    //时间格式化的函数

});

function DateFormat (datetime, formatStr) {
    var dat = datetime;
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四', '五', '六'];
    str = str.replace(/yyyy|YYYY/, dat.getFullYear());
    str = str.replace(/yy|YY/, (dat.getYear() % 100) > 9 ? (dat.getYear() % 100).toString() : '0' + (dat.getYear() % 100));
    str = str.replace(/MM/, dat.getMonth() > 9 ? (dat.getMonth() + 1).toString() : '0' + (dat.getMonth() + 1));
    str = str.replace(/M/g, (dat.getMonth() + 1));
    str = str.replace(/w|W/g, Week[dat.getDay()]);
    str = str.replace(/dd|DD/, dat.getDate() > 9 ? dat.getDate().toString() : '0' + dat.getDate());
    str = str.replace(/d|D/g, dat.getDate());
    str = str.replace(/hh|HH/, dat.getHours() > 9 ? dat.getHours().toString() : '0' + dat.getHours());
    str = str.replace(/h|H/g, dat.getHours());
    str = str.replace(/mm/, dat.getMinutes() > 9 ? dat.getMinutes().toString() : '0' + dat.getMinutes());
    str = str.replace(/m/g, dat.getMinutes());
    str = str.replace(/ss|SS/, dat.getSeconds() > 9 ? dat.getSeconds().toString() : '0' + dat.getSeconds());
    str = str.replace(/s|S/g, dat.getSeconds());
    return str
}
